package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNComment;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.utils.YNUrlHelper;

import java.util.List;

public class TimeLineCommentAdapter extends BaseAdapter {
    private Context mContext;
    private List<YNComment> mDatas;

    public TimeLineCommentAdapter(Context mContext, List<YNComment> mDatas) {
        this.mDatas = mDatas;
        this.mContext = mContext;
    }

    public int getCount() {
        return mDatas.size() > 4 ? 4 : mDatas.size();
    }

    public YNComment getItem(int position) {
        return mDatas.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from
                    (mContext).inflate(R.layout.timeline_comment_item, null, false);
            holder.avatar = (SimpleDraweeView) convertView.findViewById(R.id.comment_item_avatar);
            holder.content = (TextView) convertView.findViewById(R.id.comment_item_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        YNComment ynComment = mDatas.get(position);
        String CommentUserName = ynComment.getCommentUser().getUnickname();
        ImageOptHelper.showFrescoAvatar(Uri.parse(YNUrlHelper.getAvatarUrl(mContext, ynComment.getCommentUser().getUpic())), holder.avatar, mContext);
        String tx = CommentUserName + UIHelper.stringFilter(UIHelper.ToDBC(":" + " " + ynComment.getCcontent()));
        Spannable WordtoSpan = new SpannableString(tx);
        WordtoSpan.setSpan(new TextAppearanceSpan(mContext, R.style.item_comment_text_style4), 0, CommentUserName.length() + 1,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        WordtoSpan.setSpan(new TextAppearanceSpan(mContext, R.style.item_comment_text_style5), CommentUserName.length() + 1, tx.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.content.setText(WordtoSpan);
        return convertView;
    }

    private class ViewHolder {
        SimpleDraweeView avatar;
        TextView name;
        TextView content;
    }


}
